<template>
  <el-container class="register-view">
    <div class="register-top-banner">
      <div class="login-register-container">
        <router-link class="login-btn" to="/login">
          <i class="el-icon-back"></i>
          <div class="login-text">Login</div>
        </router-link>
        <div class="register-text">Register</div>
      </div>
      <div class="user-avatar">
        <i slot="prefix" class="el-input__icon el-icon-user-solid flex-center" v-if="defaultAvatar"></i>
        <!--          <img class="avatar-icon flex-center" v-if="defaultAvatar" alt="icon" src="../assets/image/all/register-user.png"/>-->
        <!--    Edit avatar    -->
        <RegisterAvatarEditIcon v-if="editAvatar" @clickEdit="handleClickAvatar" />
      </div>
    </div>
    <el-form class="register-form" :label-width="'0px'" :model="registerForm" status-icon
             ref="registerForm"
             @keyup.enter.native="submitForm('registerForm')">
      <el-form-item class="input-item" label="" prop="userName" :style="{'margin-top': px2rem('30px')}">
        <el-input class="no-border-input" :style="{ 'font-size': px2rem('24px') }" type="userName"
                  v-model="registerForm.userName"
                  autocomplete="off"
                  placeholder="Your Username(For login)">
          <i slot="prefix" class="el-input__icon el-icon-user-solid"></i>
        </el-input>
      </el-form-item>
      <el-form-item class="input-item" label="" prop="nickName">
        <el-input class="no-border-input" :style="{ 'font-size': px2rem('24px') }" type="nickName"
                  v-model="registerForm.nickName"
                  autocomplete="off"
                  placeholder="Your Nickname">
          <i slot="prefix" class="el-input__icon el-icon-s-custom"></i>
        </el-input>
      </el-form-item>
      <el-form-item class="input-item" label="" prop="password">
        <el-input class="no-border-input" :style="{ 'font-size': px2rem('24px') }" type="password"
                  v-model="registerForm.password"
                  show-password
                  autocomplete="off"
                  placeholder="Confirm Your Password">
          <i slot="prefix" class="el-input__icon el-icon-lock"></i>
        </el-input>
      </el-form-item>
      <el-form-item class="input-item" label="" prop="confirmPassword">
        <el-input class="no-border-input" :style="{ 'font-size': px2rem('24px') }" type="password"
                  v-model="registerForm.confirmPassword"
                  show-password
                  autocomplete="off"
                  placeholder="Your Password">
          <i slot="prefix" class="el-input__icon el-icon-lock"></i>
        </el-input>
      </el-form-item>
      <div class="register-btn-container">
        <div></div>
        <el-button class="custom-button" type="primary" @click="submitForm('registerForm')"></el-button>
        <!--        <el-button @click="resetForm('registerForm')">Reset</el-button>-->
      </div>
    </el-form>
  </el-container>
</template>

<script>
import {px2rem} from "@/utils/px2rem";
import RegisterAvatarEditIcon from "@/components/Register/RegisterAvatarEditIcon.vue";

export default {
  name: "login",
  components: {RegisterAvatarEditIcon},
  data() {
    var checkUserName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('Please input username'));
      }
      callback();
    };
    var checkNickName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('Please input nickname'));
      }
      callback();
    };
    var checkPassword = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('Please input password'));
      }
      callback();
    };

    var checkConfirmPassword = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('Please input password'));
      }
      if (value != this.registerForm.password) {
        return callback(new Error('The two password inputs are inconsistent'));
      }
      callback();
    };

    return {
      defaultAvatar: true,
      editAvatar: false,
      registerForm: {
        userName: '',
        nickName: '',
        password: '',
        confirmPassword: ''
      },
      rules: {
        userName: [{
          validator: checkUserName,
          trigger: 'blur'
        }],
        nickName: [{
          validator: checkNickName,
          trigger: 'blur'
        }],
        password: [{
          validator: checkPassword,
          trigger: 'blur'
        }],
        confirmPassword: [{
          validator: checkConfirmPassword,
          trigger: 'blur'
        }]
      }
    };
  },
  methods: {
    handleClickAvatar() {
      console.log('click avatar edit')
    },
    px2rem,
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(!this.registerForm.userName) {
            this.$message.error('Username cannot be empty');
          } else if(!this.registerForm.nickName) {
            this.$message.error('Nickname cannot be empty');
          } else if(!this.registerForm.password) {
            this.$message.error('Password cannot be empty');
          } else if(!this.registerForm.confirmPassword) {
            this.$message.error('Password cannot be empty');
          }

          if(this.registerForm.userName && this.registerForm.nickName && this.registerForm.password && this.registerForm.confirmPassword) {
            if (this.registerForm.confirmPassword != this.registerForm.password) {
              this.$message.error('The two password inputs are inconsistent')
            } else {
              this.$http({
                url: "/register",
                method: 'post',
                data: this.registerForm
              })
                  .then((data) => {
                    this.$message.success("successfully registered!");
                    this.resetForm(formName)
                  })
            }
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-icon-view:before {
  font-size: 20px;
}
.no-border-input ::v-deep .el-input__inner {
  border: none;
  border-bottom: 2px solid var(--login-form-border-color);
}

.custom-button {
  background-image: var(--login-button-image);
  background-size: cover;
  border: none;
  width: 60px;
  height: 60px;
  color: transparent;
  background-color: transparent !important;
}

.custom-button:hover,
.custom-button:active,
.custom-button:focus {
  background-image: var(--login-button-image);
  background-size: cover;
  border: none;
  width: 60px;
  height: 60px;
  color: transparent;
  background-color: transparent !important;
  outline: none;
}

.register-view {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background: var(--login-bg-color);

  .register-top-banner {
    height: 40vh;
    background-image: var(--login-banner-image);
    background-size: 100% 100%;

    .login-register-container {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin: 4vh 24px 2vh 24px;

      .login-btn {
        height: 6vh;
        border-radius: 28px;
        background: var(--register-login-bg-color);
        color: var(--register-login-color);
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 20px;
        font-size: 19px;

        .login-text {
          margin-left: 10px;
        }
      }

      .register-text {
        color: var(--login-title-color);
        opacity: 1;
        font-size: 35px;
        font-weight: 400;
        letter-spacing: 1.75px;
        line-height: 41.02px;
      }
    }

    .user-avatar {
      position: relative;
      left: 50%;
      transform: translate(-50%, 0);
      width: 16vh; // 140px
      height: 16vh;
      border-radius: 50%;
      background: var(--register-avatar-bg-color);
      color: var(--login-title-color);

      img {
        width: 100%;
        height: 100%;
      }

      .avatar-icon {
        position: relative;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -45%);
      }

      .el-input__icon {
        width: 100%;
        height: 100%;
        font-size: 12vh;
        transform: translate(0, 0vh);
      }
    }
  }

  .register-link {
    width: 100%;
    height: 50px;
    text-align: center;
    color: var(--login-active-color);
    font-size: 16px;
    border: none;
  }

  .register-btn-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .register-form {
    flex: 1;
    margin: 0 24px;
    opacity: 0.9;
    overflow-y: auto;

    .input-item {
      margin-bottom: 40px !important;
    }
  }
}

::v-deep.el-input--prefix .el-input__inner {
  padding-left: 40px !important;
}
</style>
